﻿#include "Game.h"
#include <QIcon>
#include <QPainter>
#include <QDebug>
#include <QTimer>
#include <QKeyEvent>
#include"Checkpoint.h"
#include"GameStart.h"
#include"GameOver.h"
#include<Windows.h>
#include<QDir>
void Game::LoadingResources()
{
#ifdef _DEBUG
    //手动设置目录
    m_Res->setPath("D:\\code\\CMake\\Qt\\Plane");
    OutInfo;
    qDebug() << "此处是手动设置资源路径";
#endif // !_DEBUG
    m_Res->addPixmap("背景图片", "Resource/images/background.png");
    m_Res->addPixmap("关卡背景", "Resource/images/LevelBackground.png");
    m_Res->addPixmap("游戏结束", "Resource/images/gameover.png");

    m_Res->addPixmap("玩家正常", "Resource/images/hero1.png");
    m_Res->addPixmap("玩家正常", "Resource/images/hero2.png");

    m_Res->addPixmap("玩家死亡", "Resource/images/hero_blowup_n1.png");
    m_Res->addPixmap("玩家死亡", "Resource/images/hero_blowup_n2.png");
    m_Res->addPixmap("玩家死亡", "Resource/images/hero_blowup_n3.png");
    m_Res->addPixmap("玩家死亡", "Resource/images/hero_blowup_n4.png");

    m_Res->addPixmap("玩家生命", "Resource/images/life.png");

    m_Res->addPixmap("子弹射击", "Resource/images/bullet1.png");
    m_Res->addPixmap("敌机子弹", "Resource/images/bullet2.png");
    m_Res->addPixmap("子弹补给", "Resource/images/weapon1.png");
    m_Res->addPixmap("子弹图标", "Resource/images/bullet.png");

    m_Res->addPixmap("导弹发射", "Resource/images/guidedmissile.png");
    m_Res->addMovie("导弹爆炸", "Resource/images/burst.gif");
    m_Res->addPixmap("炸弹补给", "Resource/images/weapon2.png");
    m_Res->addPixmap("炸弹图标", "Resource/images/bomb.png");

    m_Res->addPixmap("小飞机正常", "Resource/images/enemy1.png");
    m_Res->addPixmap("小飞机死亡", "Resource/images/enemy1_down1.png");
    m_Res->addPixmap("小飞机死亡", "Resource/images/enemy1_down2.png");
    m_Res->addPixmap("小飞机死亡", "Resource/images/enemy1_down3.png");
    m_Res->addPixmap("小飞机死亡", "Resource/images/enemy1_down4.png");

    m_Res->addPixmap("中飞机正常", "Resource/images/enemy2.png");
    m_Res->addPixmap("中飞机正常", "Resource/images/enemy2_hit.png");
    m_Res->addPixmap("中飞机死亡", "Resource/images/enemy2_down1.png");
    m_Res->addPixmap("中飞机死亡", "Resource/images/enemy2_down2.png");
    m_Res->addPixmap("中飞机死亡", "Resource/images/enemy2_down3.png");
    m_Res->addPixmap("中飞机死亡", "Resource/images/enemy2_down4.png");

    m_Res->addPixmap("大飞机正常", "Resource/images/enemy3_n1.png");
    m_Res->addPixmap("大飞机正常", "Resource/images/enemy3_n2.png");
    m_Res->addPixmap("大飞机正常", "Resource/images/enemy3_hit.png");
    m_Res->addPixmap("大飞机死亡", "Resource/images/enemy3_down1.png");
    m_Res->addPixmap("大飞机死亡", "Resource/images/enemy3_down2.png");
    m_Res->addPixmap("大飞机死亡", "Resource/images/enemy3_down3.png");
    m_Res->addPixmap("大飞机死亡", "Resource/images/enemy3_down4.png");
    m_Res->addPixmap("大飞机死亡", "Resource/images/enemy3_down5.png");
    m_Res->addPixmap("大飞机死亡", "Resource/images/enemy3_down6.png");

    m_Res->addPixmap("暂停按钮", "Resource/images/game_pause_nor.png");
    m_Res->addPixmap("暂停按钮", "Resource/images/game_pause_pressed.png");

    m_Res->addPixmap("继续按钮", "Resource/images/game_resume_nor.png");
    m_Res->addPixmap("继续按钮", "Resource/images/game_resume_pressed.png");

    m_Res->addPixmap("菜单按钮", "Resource/images/MenuButton1.png");
    m_Res->addPixmap("菜单按钮", "Resource/images/MenuButton2.png");
    m_Res->addPixmap("标题", "Resource/images/title.png");
    
    m_Res->addPixmap("关卡按钮", "Resource/images/fbxb1.png");
    m_Res->addPixmap("关卡按钮", "Resource/images/fbxb2.png");
}

Resource* Game::m_Res = nullptr;
Game::Game(QWidget *parent)
    : QWidget{parent}
{
    if(m_Res==nullptr)
        m_Res = new Resource;
}
Game::~Game()
{
    if (m_Res != nullptr)
    {
        delete m_Res;
        m_Res = nullptr;
    }
}
void Game::run()
{
    this->show();
}

void Game::initGame(int w, int h, const QString &title,const QIcon& icon)
{
    LoadingResources();
    setFixedSize(w,h);
    setWindowTitle(title);
    if(!icon.isNull())
    {
        setWindowIcon(icon);
    }

    if (m_game == nullptr)
        m_game = new GameStart(0, 0,this);
    QTimer* timer = new QTimer(this);
    timer->callOnTimeout(this, [=]()
        {
            //更新数据
            updateGame();
            //绘图
            update();
        });
    timer->start(1000 / 60); //游戏帧率是多少？ 60fps
}

void Game::cleanGame()
{
    qDebug() << "清理游戏";
}

void Game::updateGame()
{   if(m_game!=nullptr)
        m_game->update();
}

void Game::drawGame(QPainter *painter)
{
    if (m_game != nullptr)
        m_game->draw(painter);
}

Resource& Game::res()
{
    return *m_Res;
}
void Game::CreateGameStart()
{
    qDebug() << "开始界面";
    delete m_game;
    m_game = new GameStart(0, 0, this);

}
void Game::CreateOnPlayerLevel()
{
    qDebug() << "单人关卡";
    //if(m_game->type()== Entity::Type::gameStart)
    delete m_game;
    m_game= new Checkpoint(size(), this);
}
void Game::CreateNetworkModel()
{
    qDebug() << "网络模式";
    MessageBox((HWND)this->winId(), L"网络模式暂未开发", L"温习提示", 0);
}
void Game::CreateGameOver(const int score)
{
    qDebug() << "结束界面"<< score;
    delete m_game;
    m_game = new GameOver(score, 0, 0, this);
}

void Game::paintEvent(QPaintEvent *ev)
{
    QPainter painter(this);
    drawGame(&painter);
}

void Game::closeEvent(QCloseEvent *ev)
{
    cleanGame();
}

void Game::keyPressEvent(QKeyEvent *ev)
{
   
    /*switch (ev->key())
    {
    case Qt::Key_Up:
        m_play->setPos(0,-1* m_Steplength);
        break;
    case Qt::Key_Down:
        m_play->setPos(0, 1* m_Steplength);
        break;
    case Qt::Key_Left:
        m_play->setPos(-1 * m_Steplength,0);
        break;
    case Qt::Key_Right:
        m_play->setPos(1 * m_Steplength, 0);
        break;
    case Qt::Key_Space:
    {
        int x = m_play->getPos().x() + m_play->getSize().width() / 2;
        int y = m_play->getPos().y();
        m_playBullet->addBullet(x, y);
        break;
    }
        
    }*/
}

void Game::keyReleaseEvent(QKeyEvent *ev)
{
   /* switch (ev->key())
    {
    case Qt::Key_Up:
        play->velocity.setY(0);
        break;
    case Qt::Key_Down:
       play->velocity.setY(0);
        break;
    case Qt::Key_Left:
        play->velocity.setX(0);
        break;
    case Qt::Key_Right:
        play->velocity.setX(0);
        break;
    }*/
}
